import GameScreen from "./GameScreen";
import GameCell from "../Widget/GameCell";

const {ccclass, property} = cc._decorator;


class CellInfo
{
    public ID:number
    public Data:object
}

class PageInfo
{
    public startIndex:number
    public endIndex:number
    public idxToPos:{}
}



@ccclass
export default class DynamicSV extends GameScreen
{
    @property(cc.Prefab)
    public CellPrefab:cc.Prefab

    @property(Number)
    public PoolNum:Number

    private viewNode:cc.Node
    private contentNode:cc.Node

    private cellPool:cc.NodePool
    private pageList:PageInfo[] = []

    private beginContentPos:cc.Vec2

    private cellInfoList:CellInfo[] = []//具体数据

    private cell_window_range:[number,number]//当前滑动范围

    private maxTailIndex = 0
    initNodePool()
    {
        //一次性创建节点池
        this.cellPool = new cc.NodePool("cellpool")
        for(let i = 0;i < this.PoolNum;i++)
        {
            let node = cc.instantiate(this.CellPrefab)
            this.cellPool.put(node)
        }
        console.log("节点池一次性创建 ",this.cellPool.size()," 个节点")
    }

    onLoad()
    {
        this.viewNode = this.node.getChildByName("view")
         //自适应调整大小
        this.viewNode.setContentSize(this.node.getContentSize())

        this.contentNode = this.viewNode.getChildByName("content")
        this.contentNode.setContentSize(new cc.Size(0,0))
        this.contentNode.setPosition(new cc.Vec2(0,0))

        this.beginContentPos = this.contentNode.getPosition()
        //初始化节点池
        this.initNodePool()

        //监听滑动事件
        let scrollHandle = new cc.Component.EventHandler();
        scrollHandle.target = this.node
        scrollHandle.component = "MyInfinite"
        scrollHandle.handler =  "OnScrollEvent"
        this.getComponent(cc.ScrollView).scrollEvents.push(scrollHandle)

        this.generateTest()
    }

    onShow(param:object)
    {
        
    }

    private loadPageCell(startIndex:number,endIndex:number)
    {
        if(endIndex > startIndex)
        {

        }
    }

    private loadCellAtTail(startIndex:number,endIndex:number):PageInfo
    {
        let tailPage = new PageInfo()
        tailPage.startIndex = startIndex
        tailPage.idxToPos ={}
        let pageHeight = 0

        let beginHeight = this.contentNode.getContentSize().height
        for(let i = startIndex;i < endIndex;i++)
        {
            if (this.cellPool.size() <= 0) 
            {
                console.log("loadPageCell 对象池数据不足")
                break
            }
            //加载数据
            let node = this.cellPool.get()
            let cellObj = node.getComponent(GameCell)
            cellObj.refresh(this.cellInfoList[i].Data)

           //计算出位置
            if(tailPage.idxToPos[i] != null)
            {
               let pos:cc.Vec2 = tailPage.idxToPos[i]
               node.setPosition(pos)
            }
            else
            {
                let pos:cc.Vec2 = new cc.Vec2()
                pos.y = 0 - beginHeight + pageHeight
                tailPage.idxToPos[i] = pos
            }
            //计算页高度
            pageHeight += cellObj.node.height
            if(pageHeight > this.viewNode.getContentSize().height)break
        }

        if(endIndex > this.maxTailIndex)
        {
            this.maxTailIndex = endIndex
            this.extendContentSize(pageHeight)
        }

        return tailPage
    }

    private extendContentSize(addHeight:number)
    {
        let contentSize = this.contentNode.getContentSize()
        contentSize.height = this.contentNode.height + addHeight
        this.contentNode.setContentSize(contentSize)
    }

    private randMsg():object
    {
        let r = 1+Math.floor(Math.random()*3);
        // console.log("rand msg = "+r)
        switch(r)
        {
            case 1:
                return {"msg_type":1,"content":this.randContent()}
            case 2:
                return {"msg_type":2,"content":this.randContent()}
            case 3:
                return {"msg_type":3,"content":"21:49"}
        }
    }
    private randContent():string
    {
        let r = 1+Math.floor(Math.random()*5)
        let str = "纸条信息"
        for(let i = 0;i < r;i++)
        {
            str += str
        }
        return str
    }
    
    private generateTest()
    {
        for(let i = 0;i < 100;i++)
        {
            let cd = new CellInfo()
            cd.ID = i
            cd.Data = this.randMsg()
            cd.Data["index"] = i
            this.cellInfoList.push(cd)
        } 
    }
}